public class E0418 {

    // 1. 模拟一个trim方法，去除字符串两端的空格
    public static String trim(String str) {
        if (str == null) {
            return null;
        }
        int start = 0;
        int end = str.length() - 1;
        while (start <= end && Character.isWhitespace(str.charAt(start))) {
            start++;
        }
        while (start <= end && Character.isWhitespace(str.charAt(end))) {
            end--;
        }
        return str.substring(start, end + 1);
    }

    // 2. 将一个字符串进行反转。将字符串中指定部分进行反转。比如将“abcdefg”反转为” abfedcg”
    public static String reversePart(String str, int start, int end) {
        if (str == null || start < 0 || end >= str.length() || start > end) {
            return str;
        }
        char[] charArray = str.toCharArray();
        while (start < end) {
            char temp = charArray[start];
            charArray[start] = charArray[end];
            charArray[end] = temp;
            start++;
            end--;
        }
        return new String(charArray);
    }

    // 3. 获取一个字符串在另一个字符串中出现的次数
    public static int countOccurrences(String str, String subStr) {
        if (str == null || subStr == null || subStr.length() == 0) {
            return 0;
        }
        int count = 0;
        int index = 0;
        while ((index = str.indexOf(subStr, index)) != -1) {
            count++;
            index += subStr.length();
        }
        return count;
    }

    // 4. 获取两个字符串中最大相同子串。比如: str1= "abcwerthelloyuiodef“;str2 ="cvhellobnm"
    public static String getMaxCommonSubstring(String str1, String str2) {
        if (str1 == null || str2 == null || str1.length() == 0 || str2.length() == 0) {
            return "";
        }
        String maxSubStr = "";
        String shorter = str1.length() < str2.length() ? str1 : str2;
        String longer = str1.length() < str2.length() ? str2 : str1;
        for (int i = shorter.length(); i > 0; i--) {
            for (int j = 0; j <= shorter.length() - i; j++) {
                String subStr = shorter.substring(j, j + i);
                if (longer.contains(subStr)) {
                    return subStr;
                }
            }
        }
        return maxSubStr;
    }

    // 5. 对字符串中字符进行自然顺序排序
    public static String sortCharacters(String str) {
        if (str == null || str.length() == 0) {
            return str;
        }
        char[] charArray = str.toCharArray();
        for (int i = 0; i < charArray.length - 1; i++) {
            for (int j = 0; j < charArray.length - i - 1; j++) {
                if (charArray[j] > charArray[j + 1]) {
                    char temp = charArray[j];
                    charArray[j] = charArray[j + 1];
                    charArray[j + 1] = temp;
                }
            }
        }
        return new String(charArray);
    }

    public static void main(String[] args) {
        // 测试 1. 模拟trim方法
        String strWithSpaces = "  hello world  ";
        System.out.println("原字符串: \"" + strWithSpaces + "\"");
        System.out.println("去除两端空格后: \"" + trim(strWithSpaces) + "\"");

        // 测试 2. 字符串指定部分反转
        String strToReverse = "abcdefg";
        int start = 2;
        int end = 5;
        System.out.println("原字符串: \"" + strToReverse + "\"");
        System.out.println("指定部分反转后: \"" + reversePart(strToReverse, start, end) + "\"");

        // 测试 3. 获取子串出现次数
        String str = "abababc";
        String subStr = "ab";
        System.out.println("字符串 \"" + subStr + "\" 在 \"" + str + "\" 中出现的次数: " + countOccurrences(str, subStr));

        // 测试 4. 获取最大相同子串
        String str1 = "abcwerthelloyuiodef";
        String str2 = "cvhellobnm";
        System.out.println("最大相同子串: \"" + getMaxCommonSubstring(str1, str2) + "\"");

        // 测试 5. 对字符串字符排序
        String strToSort = "dcba";
        System.out.println("原字符串: \"" + strToSort + "\"");
        System.out.println("排序后的字符串: \"" + sortCharacters(strToSort) + "\"");
    }
}
